#ifndef _STL_IMPL_EDS_HPP_
#define _STL_IMPL_EDS_HPP_

#include <map>
#include <set>
#include <event_dispatch_struct.hpp>

template<typename T>
class stl_impl_eds: public event_dispatch_struct<T>
{
  // $$接口实现
public:
  virtual bool has_node( long _key )
  {
    const_dic_iter __iter = dic.find(_key);
    return __iter != dic.end();
  }

  virtual bool has_node( long _key, T _val )
  {
    bool __ret = false;
    if( has_node(_key) )
      {
        const elem_type &__elem = dic[_key];	//elem_type &__elem = dic[_key];
	__ret = __elem.count(_val);
      }

    return __ret;
  }

  virtual void add_node( long _key, T _val )
  {
    dic.insert(
	       typename dic_type::value_type(_key, elem_type())
	       );
    dic[_key].insert( _val );
  }

  virtual void del_node( long _key, T _val )
  {
    if( has_node(_key) )
      {
        dic[_key].erase( _val );
      }
  }

  virtual int  for_each( long _key, typename event_dispatch_struct<T>::callback_func _func, void* _callback_arg )
  {
    int ret = 0;
    if( has_node(_key) )
      {
        const elem_type &__val = dic[_key];
        for( elem_iter __iter = __val.begin();
             __iter != __val.end();
             ++__iter )
          {
            ++ret;
            _func(_callback_arg, *__iter);
          }// for
      }// if
    return ret;
  }
  
  virtual void for_each( typename event_dispatch_struct<T>::clean_func _func, void* callback_arg )
  {
    for( dic_iter __diter = dic.begin();
         __diter != dic.end();
         ++__diter )
      {
        long __id = __diter->first;
        elem_type &__val = __diter->second;
        for( elem_iter __eiter = __val.begin();
             __eiter != __val.end();
             ++__eiter )
          {
            _func(callback_arg, __id, *__eiter);
          }// for elem_iter
      }// for dic_iter
  }

  // $$内部数据结构
public:
  typedef std::set<T>                             elem_type;
  typedef std::map<long, elem_type>               dic_type;
  typedef typename elem_type::iterator            elem_iter;
  typedef typename dic_type::iterator             dic_iter;
  typedef typename dic_type::const_iterator       const_dic_iter;

  dic_type dic;
};

#endif//_STL_IMPL_EDS_HPP_
